package gamebackend;

import framework.Color;
import generators.BasicBlockGenerator;
import generators.BlockRule;
import gui.BlockStyle;

import java.util.Random;

public class GateBiome extends GameSession.Biome{
	/**
	 * How close the paths need to be to their destinations to be considered at
	 * their destinations.
	 */
	private static final int TOLLERANCE = 1;
	private static final double MIN_GATE_LENGTH = 100;
	/**
	 * I can't tell generators when to die,
	 * so I'll just create generators at a fixed interval.
	 */
	private static final int GENERATOR_LENGTH = 20;
	private static final int BACKGROUND_SKIP = 2;
	private static final Block GRAY = new Block(BlockStyle.METAL, new Color(75, 75, 70));
	private static final Block GREEN = new Block(BlockStyle.METAL, new Color(90, 125, 70));
	private static final Block BLUE = new Block(BlockStyle.METAL, new Color(70, 100, 250));
	private static final Block[] BLOCKS = {GRAY, GREEN, BLUE};
	private double my_hardTarget;
	private double my_easyTarget;
	private boolean my_gate;
	private double my_lengthBeforeGenerator;
	private double my_gateLength;
	private int my_backgroundSkipLeft;

	public GateBiome(GameSession the_session,
			int the_tier, Random the_random, double the_length) {
		the_session.super(the_session, the_tier, the_random, the_length);
		my_hardTarget = getWidth() / 2;
		my_easyTarget = getWidth() / 2;
		addEasyPath(new FlightPath(Block.PRIMARY_PATH, getEasyPath(), 40), getLength());
		addHardPath(new FlightPath(Block.SECONDARY_PATH, getHardPath(), 20 - getTier() * 3), getLength());
		getEasyPath().getShipFlier().setTarget(my_easyTarget);
		getHardPath().getShipFlier().setTarget(my_hardTarget);
	}
	public void step(final double the_yChange) {
		super.step(the_yChange);
		my_gateLength += the_yChange;
		if (my_lengthBeforeGenerator < 0) {
			my_lengthBeforeGenerator += GENERATOR_LENGTH;
			if (my_gate) {
				final Block block = BLOCKS[getRandom().nextInt(BLOCKS.length)];
				addGenerator(new BasicBlockGenerator(block, BlockRule.EMPTY, getRandom(), 1), GENERATOR_LENGTH * 1.5);
				if (my_backgroundSkipLeft == 0) {
					my_backgroundSkipLeft = BACKGROUND_SKIP + 1;
					addToMidGround(new BasicBlockGenerator(
							new Block(BlockStyle.METAL, Block.getBackgroundColor(block.getColor())),
							BlockRule.ANY, getRandom(), 1), GENERATOR_LENGTH * 1.5);
				}
				my_backgroundSkipLeft--;
				
			}
		}
		my_lengthBeforeGenerator -= the_yChange;
		if (getRemaining() > MIN_GATE_LENGTH
				&& Math.abs(getEasyPath().getXPosition() - my_easyTarget) < TOLLERANCE
				&& Math.abs(getHardPath().getXPosition() - my_hardTarget) < TOLLERANCE
				&& (!my_gate || my_gateLength > MIN_GATE_LENGTH)) {
			my_gate = !my_gate;
			if (my_gate) {
				my_backgroundSkipLeft = 0;
				my_gateLength = 0;
			}
			
			if (!my_gate || getRandom().nextDouble() < getTier() * .15 + .1) {
				my_easyTarget = (my_easyTarget + getWidth() - 100) % (getWidth() - 40) + 20;
			}
			if (!my_gate || getRandom().nextDouble() < getTier() * .3 + .1) {
				my_hardTarget = (my_hardTarget + 100) % (getWidth() - 16) + 8;
			}
			getEasyPath().getShipFlier().setTarget(my_easyTarget);
			getHardPath().getShipFlier().setTarget(my_hardTarget);
//			my_easyFlier.setTarget(my_easyTarget);
//			my_hardFlier.setTarget(my_hardTarget);
		}
	}

}
